package com.fmsz.xmly.service;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;

import java.io.IOException;

public class PlayService extends Service {
    private MediaPlayer player;
    private Handler handler;
    private Thread thread;

    public void setHandler(Handler handler) {
        this.handler = handler;
    }


    public PlayService() {
    }

    @Override
    public void onCreate() {
        super.onCreate();
        player = new MediaPlayer();
        player.setLooping(true);
        player.setOnPreparedListener(new MyPrepareListener());
        player.setOnCompletionListener(new MyCompletionListener());
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
//        throw new UnsupportedOperationException("Not yet implemented");
        return new MyBinder();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public boolean onUnbind(Intent intent) {
        handler = null;
        return super.onUnbind(intent);
    }

    public void loadMusic(String musicUrl) {
        if (player != null) {
            if (player.isPlaying()) {
                player.stop();
            }
            player.reset();
            try {
                player.setDataSource(musicUrl);
            } catch (IOException e) {
                e.printStackTrace();
            }
            player.prepareAsync();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        handler = null;
        stop();
    }

    //暂停播放
    public void pause() {
        if (player.isPlaying()) {
            player.pause();
        } else {
            if (player != null && !player.isPlaying()) {
                start();
            }
        }
    }

    //开始播放
    private void start() {
        player.start();
    }

    //停止播放
    private void stop() {
        if (player != null) {
            player.release();
            player = null;
        }
    }

    //指定位置播放
    public void seekToPorgress(int duration) {
        if (player != null) {
            if (duration > 0 && duration < player.getDuration()) {
                player.seekTo(duration * 1000);
            }
        }
    }


    public class MyBinder extends Binder {
        public PlayService getService() {
            return PlayService.this;
        }
    }

    //音乐播放准备监听
    private class MyPrepareListener implements MediaPlayer.OnPreparedListener {

        @Override
        public void onPrepared(final MediaPlayer mp) {
            mp.start();
            thread = new Thread(new Runnable() {
                @Override
                public void run() {
                    out:
                    while (player != null) {
                        while (player.isPlaying()) {
                            if (handler == null) {
                                break out;
                            }
                            Message message = Message.obtain();
                            message.arg1 = player.getCurrentPosition();
                            handler.sendMessage(message);
                            try {
                                Thread.sleep(1000);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }
            });
            thread.start();
        }
    }

    private class MyCompletionListener implements MediaPlayer.OnCompletionListener {

        @Override
        public void onCompletion(MediaPlayer mp) {
            handler.sendEmptyMessage(1);
        }
    }
}
